<template>
    <table border="1" class="table table-bordered">
        <tr>
            <td>公告标题</td>
            <td>
                <input type="text" v-model="info.ntitle">
            </td>
        </tr>
        <tr>
            <td>公告内容</td>
            <td>
                <textarea v-model="info.ncontent">

                </textarea>
            </td>
        </tr>
        <tr>
            <td>公告状态</td>
            <td>
                <input type="radio" :value="true" v-model="info.nstate">显示
                <input type="radio" :value="false" v-model="info.nstate">隐藏
            </td>
        </tr>
        <tr>
            <td>发布人</td>
            <td>
                <input type="text" v-model="info.sendpeo">
            </td>
        </tr>
        <tr>
            <td>发起时间</td>
            <td>
                <input type="date" v-model="info.createtime">
            </td>
        </tr>
        <tr>
            <td>公告类型</td>
            <td>
                <select v-model="info.ntid">
                    <option :value="-1">--请选择--</option>
                    <option v-for="item in bindt" :value="item.ntid">{{item.ntname}}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center;">
                <input type="button" value="保存" @click="add">
            </td>
        </tr>
    </table>
</template>
<script setup lang="ts">
import {ref} from 'vue'
import axios from 'axios';
import { onMounted } from 'vue';
import { useRouter } from 'vue-router';
onMounted(()=>{
    bind();
})
const bindt=ref([{
    "ntid": 0,
    "ntname": ""
}])
const bind=()=>{
    axios.get('http://localhost:63668/api/Notice/Bind_t')
    .then(res=>{
        bindt.value=res.data
    })
    .catch(error=>{
        console.log(error);
    })
}
const info=ref({
    "nid": 0,
    "ntitle": "",
    "ncontent": "",
    "nstate": true,
    "sendpeo": "",
    "createtime": "",
    "ntid": ""
})
const ror=useRouter();
const add=()=>{
    if(info.value.ntitle==""){
        alert('标题不能为空');
        return;
    }
    var reg=/^[\u4e00-\u9fa5]{1,20}$/;
    if(!reg.test(info.value.ntitle)){
        alert('公告标题必须是中文');
        return;
    }
    if(info.value.ntitle.length>20){
        alert('公告标题必须是20以内的中文汉字');
        return;
    }
    if(info.value.nstate==null){
        alert('状态不能为空');
        return;
    }
    if(info.value.ntid==""){
        alert('类型不能为空');
        return;
    }
    if(info.value.ncontent.length>50){
        alert('公告标题内容不大于50个字');
        return;
    }
    axios.post('http://localhost:63668/api/Notice/AddNotice',info.value)
    .then(res=>{
        if(res.data>0){
            alert('保存成功');
            ror.push('/ShowNotice');
        }
        else{
            alert('保存失败');
        }
    })
    .catch(error=>{
        console.log(error);
    })
}
</script>